#include "eventfilter.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QEvent>
#include <QMouseEvent>

//主要讲述了如何实现图片放大的事件过滤。首先，介绍了使用q matrix类来操作图片，并通过设置比率和调用scale方法来放大图片。
//然后，解释了如何判断用户点击的是哪张图片，并处理鼠标按下事件。在事件处理中，需要将事件转换为鼠标事件，并判断按下的按钮是左键还是中键，
//从而更新标签的文本。最后，强调了这只是演示如何过滤事件，企业开发中可能更复杂。

EventFilter::EventFilter(QWidget *parent,Qt::WindowFlags f)
    : QDialog(parent,f)
{
    setWindowTitle(QString("事件过滤"));
    m_label1 =new QLabel;
    m_label2 =new QLabel;
    m_label3 =new QLabel;
    m_stateLabel = new QLabel(QString("鼠标按下标志"));
    m_stateLabel->setAlignment(Qt::AlignHCenter);
    m_stateLabel->setMargin(200);

    m_image1.load("fly1.png");
    m_image2.load("fly2.png");
    m_image3.load("fly3.png");
    m_label1->setPixmap(QPixmap::fromImage(m_image1));
    m_label2->setPixmap(QPixmap::fromImage(m_image2));
    m_label3->setPixmap(QPixmap::fromImage(m_image3));

    QHBoxLayout *layout =new QHBoxLayout;
    layout->addWidget(m_label1);
    layout->addWidget(m_label2);
    layout->addWidget(m_label3);

    QVBoxLayout *mainlayout =new QVBoxLayout(this);
    mainlayout->addLayout(layout);
    mainlayout->addWidget(m_stateLabel);

    resize(m_image2.width() *3 ,m_image2.height() *2);

    //给图片标签安装事件过滤，指定整个窗体为监视事件的对象
    m_label1->installEventFilter(this);
    m_label2->installEventFilter(this);
    m_label3->installEventFilter(this);


}

EventFilter::~EventFilter()
{

}

bool EventFilter::eventFilter(QObject *watched, QEvent *event)
{

    QMatrix matrix;
    QImage tmpImg;

    //放大或缩小图片尺寸
    matrix.scale(2.0,2.0);
    if(watched == m_label1)
    {
        //鼠标按下事件的处理
        if(event->type()==QEvent::MouseButtonPress)
        {
            QMouseEvent *mouseEvent = (QMouseEvent*)event;
            if(mouseEvent->buttons()& Qt::LeftButton)
                m_stateLabel->setText(QString("左键按下图片1"));
            if(mouseEvent->buttons()& Qt::MidButton)
                m_stateLabel->setText(QString("中键按下图片1"));
            if(mouseEvent->buttons()& Qt::RightButton)
                m_stateLabel->setText(QString("右键按下图片1"));

            tmpImg = m_image1.transformed(matrix);
            m_label1->setPixmap(QPixmap::fromImage(tmpImg));
        }

        //鼠标释放，恢复图片大小
        if(event->type() == QEvent::MouseButtonRelease)
        {
            m_label1->setPixmap(QPixmap::fromImage(m_image1));
        }
    }

    else if(watched == m_label2)
    {
        //button返回“发生了此事件的按钮”，buttons返回"发生事件时还处于按下状态的按钮"
        //Qt::NoButton      0x00000000
        //Qt::LeftButton    0x00000001
        //Qt::RightButton   0x00000002
        //Qt::MidButton     0x00000004
        //当左键按下时，返回值是1; 右键 2; 中键 4; 左+右 3；左+中 5；右+中 6; 左右中 7
        //(Event->buttons() & Qt::LeftButton) 判断左键是否被按下，只有左键按下了，其返回值就是1(true) //&为位运算符，按位进行与运算。


        if(event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *mouseEvent = (QMouseEvent*)event;
            if(mouseEvent->buttons() &Qt::LeftButton)
                m_stateLabel->setText(QString("左键按下图片2"));
            if(mouseEvent->buttons() & Qt::MidButton)
                m_stateLabel->setText(QString("中键按下图片2"));
            if(mouseEvent->buttons() & Qt::RightButton)
                m_stateLabel->setText(QString("右键按下图片2"));

            tmpImg = m_image2.transformed(matrix);
            m_label2->setPixmap(QPixmap::fromImage(tmpImg));
        }

        if(event->type() == QEvent::MouseButtonRelease)
        {
            m_label2->setPixmap(QPixmap::fromImage(m_image2));
        }
    }
    else if(watched == m_label3)
    {
        if(event->type()==QEvent::MouseButtonPress)
        {
            QMouseEvent *mouseEvent = (QMouseEvent*)event;
            if(mouseEvent->buttons() & Qt::LeftButton)
                m_stateLabel->setText(QString("左键按下图片3"));
            if(mouseEvent->buttons() & Qt::MidButton)
                m_stateLabel->setText(QString("中键按下图片3"));
            if(mouseEvent->buttons() & Qt::RightButton)
                m_stateLabel->setText(QString("右键按下图片3"));

            tmpImg =m_image3.transformed(matrix);
            m_label3->setPixmap(QPixmap::fromImage(tmpImg));
        }

        if(event->type() == QEvent::MouseButtonRelease)
        {
            m_label3->setPixmap(QPixmap::fromImage(m_image3));
        }
    }


    //事件交给上层对话框进行处理
    return QDialog::eventFilter(watched,event);
}






